******************************************
* Time preferences and credit constraints
* Conditional logit estimates
******************************************

//A Sautmann May 6 2018


clear all
// Change the path below to data folder in the format "User/data"

global datafolder "User/data"
cd $datafolder

version 14.2

// *****************************************************************************
 // Decision A
 use "${datafolder}/TP-CLformat.dta" , clear
 
 forvalues t=2/4 {
 // drop persons with more than 3 switches
	drop if TPwk`t'_Aswitch>3
 }
 
 // drop all persons with missing choices or financial vars
egen temp =rownonmiss(TPd*) 
	drop if temp<24
	drop temp

// drop people whose preferences cannot be identified because they are at the 
// corners (all identical decisions)
egen sumA=rowtotal(TPd??)
	drop if sumA==0 | sumA==24
	drop sumA

// create long format with each week a row; create week indicator
reshape long TPd1 TPd2 TPd3 TPd4 TPd5 TPd6 TPd7 TPd8 Event EventSpend Income ///
Spending SpendingNonEvent EarningsNonLaborEnd EarningsNonLaborEx EarningsLabor ///
 SpendNec  LgPurchSpend  SpendBills SpendGifts  SpendFunH SpendSocialEvent, i(person) j(week)

// implied MRS cutoff values
forvalues k=1/8 {
	gen dec`k'= 300/(50*`k')
}
 
label values week .
label var week "Survey week"
 
// create long format with each decision a row; create row indicator
reshape long TPd dec, i(person week) j(row)

 label var row "Row number in decision list"

 label values TPd .
 label var TPd "Binary choice earlier vs. later payment"
 
 gen week2=(week==2)
 gen week3=(week==3)
 gen week4=(week==4)
 
// invert variables to match logit expression
foreach var in Event EventSpend Spending Income SpendingNonEvent EarningsNonLaborEnd EarningsNonLaborEx EarningsLabor SpendNec LgPurch SpendBills SpendGi SpendFun SpendSocial {
	replace `var' = -`var'
}
// created squared variables
foreach var in EventSpend SpendingNonEvent EarningsNonLaborEnd EarningsNonLaborEx EarningsLabor  {
	gen `var'2 = - `var'^2
}


// Note: coefficient on dec is 1/sigma of the error term!

// Table 5: Income	
clogit TPd dec Income  week3 week4 , group(person) vce(cluster person)
clogit TPd dec EarningsLabor EarningsNonLaborEnd EarningsNonLaborEx  week3 week4 , group(person) vce(cluster person)

// Table 6: preference shocks
clogit TPd dec Event  week3 week4, group(person) vce(cluster person)
clogit TPd dec EventSpend  week3 week4, group(person) vce(cluster person)
  
// Table 8: spending
clogit TPd dec Spending  week3 week4, group(person) vce(cluster person)
clogit TPd dec SpendNec EventSpend LgPurch SpendBills SpendGi SpendFun SpendSocial  week3 week4, group(person) vce(cluster person)


// Table 14: all shocks
clogit TPd dec EventSpend SpendingNonEvent EarningsNonLaborEnd EarningsNonLaborEx EarningsLabor week3 week4, group(person) vce(cluster person)

// Table 15: quadratics
clogit TPd  EarningsNonLaborEx EarningsNonLaborEx2  EventSpend EventSpend2  ///
EarningsNonLaborEnd EarningsNonLaborEnd2  EarningsLabor EarningsLabor2 ///
SpendingNonEvent SpendingNonEvent2 dec ///
 week3 week4, group(person) vce(cluster person)

 test EarningsNonLaborEx EarningsNonLaborEx2
 test EventSpend EventSpend2
 test EarningsNonLaborEx2 EventSpend2 SpendingNonEvent2 EarningsNonLaborEnd2 EarningsLabor2 

